<script lang="ts">
import dayjs from 'dayjs'
import { Component, Vue } from 'vue-property-decorator'

import { userStore } from '@/store'
import { showToast, sleep, toPromise } from '@/utils'
import { postClockInApi, getClockInRecordsApi } from './service'

@Component({ name: 'ClockIn' })
export default class ClockIn extends Vue {
  address = ''
  latitude = 0
  longitude = 0
  dataList: any[] = []
  async onLoad() {
    const { latitude, longitude } = await toPromise(uni.getLocation, {
      type: 'gcj02',
      isHighAccuracy: false
    })
    this.latitude = latitude
    this.longitude = longitude
    const { records } = await getClockInRecordsApi({
      OfficerID:
        this.userInfo.Permission === 'user' ? this.userInfo.OfficerID : ''
    })
    this.dataList = records.map((i: any) => {
      return {
        ...i,
        CheckInTime: dayjs(i.CheckInTime).format('YYYY-MM-DD HH:mm:ss')
      }
    })
  }
  async chooseLocations() {
    uni.chooseLocation({
      latitude: this.latitude,
      longitude: this.longitude,
      success: async ({ address }) => {
        this.address = address
      },
      fail: console.log
    })
  }
  currentTime = dayjs().valueOf()
  showtime = false
  timeValue = dayjs().valueOf()
  handleConfirmChooseDateRange({ value }: Recordable) {
    this.timeValue = value
    this.showtime = false
  }
  get getTime() {
    return dayjs(this.timeValue).format('YYYY-MM-DD HH:mm')
  }
  get userInfo(): any {
    return userStore.userInfo || {}
  }
  isClockIn = false
  async handleClock() {
    if (!this.address) {
      showToast({ title: '请选择工作地点' })
      return
    }
    try {
      uni.showLoading({ title: 'loadng...' })
      await postClockInApi({
        OfficerID: this.userInfo.OfficerID,
        CheckInTime: this.getTime,
        Location: this.address
      })
      this.isClockIn = true
      await sleep(1000)
      uni.navigateBack()
    } finally {
      uni.hideLoading()
    }
  }
}
</script>

<template>
  <view class="page-container">
    <view class="card_init">
      <view
        class="flex justify-between items-center text-base py-20rpx field-item"
      >
        <text>工作开始时间：</text>
        <view
          class="text-third text-sm flex items-center"
          @click="showtime = true"
        >
          <text class="mr-4rpx">{{ getTime || '点击选择' }}</text>
          <u-icon name="arrow-down" color="#999" size="10"></u-icon>
        </view>
      </view>
      <view class="punch-in-box">
        <view v-if="!isClockIn" class="punch-in-btn" @click="handleClock">
          <view>打卡上岗</view>
        </view>
        <view v-else class="success-result-box">
          <text class="tips">上岗成功</text>
        </view>
        <view class="address-info flex items-center" @click="chooseLocations">
          <u-icon name="map" color="#999" size="12"></u-icon>
          <text class="ml-4rpx text-sm">{{ address || '选择工作地点' }}</text>
        </view>
      </view>
    </view>
    <u-divider text="打卡记录"></u-divider>
    <view v-if="dataList.length">
      <view
        v-for="i in dataList"
        :key="i.AttendanceID"
        class="rounded-4px text-first text-14px p-10px -mx-15px"
        style="border-bottom: 1px #ccc solid"
      >
        <view>姓名：{{ i.OfficerName }}</view>
        <view class="my-10px">时间：{{ i.CheckInTime }}</view>
        <view style="line-height: 18px">地点：{{ i.Location }}</view>
      </view>
    </view>
    <u-empty v-else mode="data" />
    <u-datetime-picker
      v-model="timeValue"
      mode="datetime"
      :show="showtime"
      :minDate="currentTime"
      @confirm="handleConfirmChooseDateRange"
    ></u-datetime-picker>
  </view>
</template>

<style scoped lang="scss">
.card_init {
  background-color: white;
  border-radius: 16rpx;
  min-height: 100rpx;
  padding: 20rpx;
}

.field-item:not(:last-child) {
  border-bottom: 1rpx solid #eeeeee;
}

.input-card {
  margin-top: 30rpx;
  padding: 0 30rpx;

  .field-item {
    position: relative;
    border-bottom: none;

    &:not(:last-child)::after {
      content: '';
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: 0;
      width: calc(100% - 64rpx);
      height: 1px;
      background-color: #e5e5e5;
    }

    .value {
      color: #333;
      text-align: right;
      font-size: 28rpx;
      line-height: 28rpx;
      /* 100% */

      &.active {
        color: #28c141;
      }

      &.select {
        padding-right: 32rpx;

        &::after {
          content: '';
          position: absolute;
          right: 32rpx;
          top: 50%;
          transform: translateY(-50%);
          width: 18rpx;
          height: 9rpx;
          background-size: 100% 100%;
        }
      }

      &.is-placeholder {
        color: #c5c5c5;
      }
    }
  }

  .input-card-wrapper {
    border-radius: 16rpx;
    overflow: hidden;
    background-color: #ffffff;
  }
}

.punch-in-box {
  padding: 100rpx 0 40rpx;
  text-align: center;

  .punch-in-btn {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    width: 214rpx;
    height: 214rpx;
    border-radius: 999rpx;
    background: linear-gradient(180deg, #8fbafc 0%, #4a91ff 100%);
    box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.1);
    font-size: 32rpx;
    color: #ffffff;

    &:active,
    &:focus {
      background: linear-gradient(180deg, #4a91ff 0%, #8fbafc 100%);
    }
  }

  .success-result-box {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    width: 214rpx;
    height: 214rpx;
    border-radius: 999rpx;
    background: linear-gradient(180deg, #0eb841 0%, #21b849 100%);
    box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.1);
    font-size: 32rpx;
    color: #ffffff;

    &:active,
    &:focus {
      background: linear-gradient(180deg, #0eb841 0%, #21b849 100%);
    }
  }
  .address-info {
    text-align: left;
    margin-top: 90rpx;
    color: #999;
    font-size: 28rpx;
    line-height: 36rpx;
    position: relative;

    &::before {
      content: ' ';
      position: absolute;
      left: 0;
      top: 50%;
      width: 32rpx;
      height: 32rpx;
      transform: translateY(-50%);
      background-size: 100% 100%;
    }
  }
}

.qr-title {
  padding: 32rpx;
  color: #333;
  font-size: 32rpx;
  line-height: 28rpx;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 32rpx;
    width: 18rpx;
    height: 9rpx;
    background-size: 100% 100%;
    transform-origin: center center;
  }

  &.expand {
    &::after {
      transform: translateY(-50%) rotate(180deg);
    }
  }
}

.qr-code-box {
  width: 450rpx;
  height: 450rpx;
  margin: 12rpx auto;

  .qr-code {
    width: 100%;
    height: 100%;
  }
}

.success-result-box {
  margin: 0 auto;
  width: 214rpx;
  height: 214rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  image {
    width: 90rpx;
    height: 90rpx;
  }

  .tips {
    font-size: 28rpx;
    color: #fff;
  }
}
</style>
